<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>四川非遗 - 用户系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/public.css">
</head>
<body class="bg-gray-100">
    <div class="min-h-screen flex items-center justify-center">
        <div class="bg-white p-8 rounded-lg shadow-lg w-96">
            <div class="text-center mb-8">
                <h1 class="text-2xl font-bold text-gray-800">四川非遗</h1>
                <p class="text-gray-600">用户登录/注册系统</p>
            </div>

            <!-- 登录表单 -->
            <div id="loginForm" class="space-y-4">
                <h2 class="text-xl font-semibold text-gray-700 mb-4">登录</h2>
                <div>
                    <label class="block text-gray-700">用户名</label>
                    <input type="text" id="loginUsername" class="w-full p-2 border rounded mt-1">
                </div>
                <div>
                    <label class="block text-gray-700">密码</label>
                    <input type="password" id="loginPassword" class="w-full p-2 border rounded mt-1">
                </div>
                <button onclick="login()" class="w-full bg-blue-500 text-white p-2 rounded hover:bg-blue-600">
                    登录
                </button>
                <p class="text-center text-gray-600">
                    还没有账号？
                    <a href="#" onclick="toggleForms()" class="text-blue-500 hover:underline">注册新账号</a>
                </p>
            </div>

            <!-- 注册表单 -->
            <div id="registerForm" class="space-y-4 hidden">
                <h2 class="text-xl font-semibold text-gray-700 mb-4">注册</h2>
                <div>
                    <label class="block text-gray-700">用户名</label>
                    <input type="text" id="registerUsername" class="w-full p-2 border rounded mt-1">
                </div>
                <div>
                    <label class="block text-gray-700">密码</label>
                    <input type="password" id="registerPassword" class="w-full p-2 border rounded mt-1">
                </div>
                <div>
                    <label class="block text-gray-700">确认密码</label>
                    <input type="password" id="confirmPassword" class="w-full p-2 border rounded mt-1">
                </div>
                <button onclick="register()" class="w-full bg-green-500 text-white p-2 rounded hover:bg-green-600">
                    注册
                </button>
                <p class="text-center text-gray-600">
                    已有账号？
                    <a href="#" onclick="toggleForms()" class="text-blue-500 hover:underline">返回登录</a>
                </p>
            </div>
        </div>
    </div>

    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script src="/static/js/jquery.js"></script>
    <script>
        // 切换登录和注册表单
        function toggleForms() {
            document.getElementById('loginForm').classList.toggle('hidden');
            document.getElementById('registerForm').classList.toggle('hidden');
        }

        // 登录函数
        async function login() {
            const username = document.getElementById('loginUsername').value;
            const password = document.getElementById('loginPassword').value;

            try {
                const response = await fetch('http://127.0.0.1:5001/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password })
                });

                const data = await response.json();

                if (response.ok) {
                    // 保存token
                    localStorage.setItem('token', data.token);
                    alert('登录成功！');
                    // 登录成功后跳转到主页面
                    window.location.href = '文创商城.html';
                } else {
                    alert(data.message || '登录失败，请检查用户名和密码');
                }
            } catch (error) {
                alert('登录失败，请稍后重试');
                console.error('Error:', error);
            }
        }

        // 注册函数
        async function register() {
            const username = document.getElementById('registerUsername').value;
            const password = document.getElementById('registerPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }

            try {
                const response = await fetch('http://127.0.0.1:5001/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password })
                });

                const data = await response.json();

                if (response.ok) {
                    alert('注册成功！请登录');
                    toggleForms(); // 切换到登录表单
                } else {
                    alert(data.message || '注册失败，请稍后重试');
                }
            } catch (error) {
                alert('注册失败，请稍后重试');
                console.error('Error:', error);
            }
        }
    </script>
</body>
</html> 